//
//  EditImageViewController.m
//  FMRecordVideo
//
//  Created by 高波 on 2019/4/15.
//  Copyright © 2019 SF. All rights reserved.
//

#import "EditImageViewController.h"
#import "HFImageEditorFrameView.h"
@interface EditImageViewController () <UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet HFImageEditorFrameView *frameView;


@end

@implementation EditImageViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        CGFloat width = kScreenWidth - 20 * 2;
        CGFloat height = kScreenHeight - 2 * 46 - 2 * 40;
        self.cropRect = CGRectMake((kScreenWidth - width)/2, 20, width, height);
        self.minimumScale = 0.2;
        self.maximumScale = 3;
    }
    return self;
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGFloat width = kScreenWidth - 20 * 2;
    CGFloat height = kScreenHeight - 2 * 46 - 2 * 40;
    self.cropRect = CGRectMake((kScreenWidth - width)/2, 20, width, height);
    [self reset:YES];
}

#pragma mark - UICollectionViewDelegate
- (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{
    
    newSize.height=image.size.height*(newSize.width/image.size.width);   //确保新图片的高等比例缩放，不会造成变形
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
    
}
- (UIImage *)clipWithImageRect:(CGRect)imageRect clipRect:(CGRect)clipRect clipImage:(UIImage *)clipImage;

{
    
    // 开启位图上下文
    
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0);
    
    // 设置裁剪区域
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:clipRect];
    
    [path addClip];
    
    // 绘制图片
    
    [clipImage drawInRect:clipRect];
    
    // 获取当前图片
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭上下文
    
    UIGraphicsEndImageContext();
    
    return image;
    
}


@end
